﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using database.Entities;
using System.Data.OracleClient;
using System.Data;
using System.Collections;

namespace database.DAOImpl
{
    class StoreDAOImpl
    {

        public StoreDAOImpl()
        {

        }

        public void insertIntoStore(Store s)
        {
            DBUtils util = new DBUtils();
            util.ConnOpen();
            string sql = "insert into STORES (OWNER, RENT, CONTRACT, ADDRESS, STORENAME) values (:owner, :rent, :contract, :address, :storename)";
            OracleCommand cmd = new OracleCommand(sql, util.getConn());
            OracleParameter parOwner = new OracleParameter(":owner", s.Owner);
            OracleParameter parRent = new OracleParameter(":rent", s.Rent);
            OracleParameter parContract = new OracleParameter(":contract", s.Contract);
            OracleParameter parAddress = new OracleParameter(":address", s.Address);
            OracleParameter parStoreName = new OracleParameter(":storename", s.StoreName);

            cmd.Parameters.Add(parOwner);
            cmd.Parameters.Add(parRent);
            cmd.Parameters.Add(parContract);
            cmd.Parameters.Add(parAddress);
            cmd.Parameters.Add(parStoreName);

            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            util.ConnClose();
        }

        public void updateByStoreId(int id)
        {
            DBUtils util = new DBUtils();
            util.ConnOpen();
            string sql = "UPDATE USERS SET USERNAME = :username WHERE USERID = :id";
            util.ConnClose();

        }

        public void deleteByStoreID(int id)
        {

        }

        public Store selectByStoreId(int id)
        {
            DBUtils util = new DBUtils();
            util.ConnOpen();
            string sql = "select *from STORES where STOREID = :storeId";
            OracleCommand cmd = new OracleCommand(sql, util.getConn());
            OracleParameter parId = new OracleParameter(":storeId", id);
            cmd.Parameters.Add(parId);
            cmd.CommandText = sql;
            OracleDataReader odr = cmd.ExecuteReader();
            Store s = new Store();

            if (odr.Read())
            {
                s.StoreID = odr.GetInt32(0);
                s.Owner = odr.GetInt32(1);
                s.Rent = odr.GetInt32(2);
                s.Contract = odr.GetString(3);
                s.Address = odr.GetString(4);
                s.StoreName = odr.GetString(5);
            }

            util.ConnClose();

            return s;
        }

        public Store selectByStoreName(string name)
        {
            DBUtils util = new DBUtils();
            util.ConnOpen();
            string sql = "select *from STORES where STORENAME = :storeName";
            OracleCommand cmd = new OracleCommand(sql, util.getConn());
            OracleParameter parStoreName = new OracleParameter(":storeName", name);
            cmd.Parameters.Add(parStoreName);
            OracleDataReader odr = cmd.ExecuteReader();
            Store s = new Store();

            if (odr.Read())
            {
                s.StoreID = odr.GetInt32(0);
                s.Owner = odr.GetInt32(1);
                s.Rent = odr.GetInt32(2);
                s.Contract = odr.GetString(3);
                s.Address = odr.GetString(4);
                s.StoreName = odr.GetString(5);
            }

            util.ConnClose();

            return s;
        }
    }
}
